package com.kehanpay.sdk.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kehanpay.sdk.Kehan;
import com.kehanpay.sdk.util.KehanSignUtil;

/**
 * Servlet implementation class CnbcPayWeixinNativeNotify
 */
public class KehanpayNotify extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	/**
     * URLEncoder charset
     */
    public static final String CHARSET = "UTF-8";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public KehanpayNotify() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			/**header头信息*/
			//应用appId
			String appId = request.getHeader("appId");
			//获取kehanpay通知的加密字符串
			String kehan_signature = request.getHeader("kehan-signature");
			
			/**body信息*/
			//支付通道代码
			String trade_channel = request.getParameter("trade_channel");
			//Kehanpay支付凭证ID
			String trade_charge_id = request.getParameter("trade_charge_id");
			//商户订单号
			String trade_order_no = request.getParameter("trade_order_no");
			//订单总金额
			String trade_order_amount = request.getParameter("trade_order_amount");
			//第三方通道交易流水号
			String trade_third_number = request.getParameter("trade_third_number");
			//支付结果 
			String trade_result_code = request.getParameter("trade_result_code");
			//支付完成时间
			String trade_time_end = request.getParameter("trade_time_end");
			
			//---
			Map<String,Object> notifydata = new HashMap<String, Object>();
			notifydata.put("trade_channel", trade_channel);
			notifydata.put("trade_charge_id", trade_charge_id);
			notifydata.put("trade_order_no", trade_order_no);
			notifydata.put("trade_order_amount", trade_order_amount);
			notifydata.put("trade_third_number", trade_third_number);
			notifydata.put("trade_result_code", trade_result_code);
			notifydata.put("trade_time_end", trade_time_end);
			//===
			notifydata = KehanSignUtil.sortMapByKey(notifydata);
			
			String query = KehanSignUtil.createJSONString(notifydata);
			    
			Kehan.privateKey = "";
			Kehan.privateKeyPath = this.getClass().getClassLoader().getResource("/").getPath()+"res/your_rsa_private_key.pem";;
			
			FileInputStream inputStream = new FileInputStream(Kehan.privateKeyPath);
			
			byte[] keyBytes = new byte[inputStream.available()];
			inputStream.read(keyBytes);
			inputStream.close();
			String trimmedPrivateKey = new String(keyBytes, CHARSET);
			
			String mysignature = KehanSignUtil.generateSign(query,trimmedPrivateKey);
			if(kehan_signature.equals(mysignature)){
				/**
				 * 你的业务逻辑代码
				 * */
				System.out.print("你的业务逻辑代码......");
				if(trade_result_code.equals("SUCCESS")){
					
				}else{
					
				}
			}else{
				System.out.println("签名验证错误，kehanpay服务端和本地签名不一致。");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			response.getWriter().print("success");
		}
	}
}
